package com.changgou.goods.controller;


import com.changgou.goods.service.BrandService;
import com.changgou.goods.pojo.Brand;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
@CrossOrigin
public class BrandController {
    @Autowired
    private BrandService brandService;

    /***
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result<Brand> findAll() {
        List<Brand> brandList = brandService.findAll();
        return new Result<Brand>(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping("{brandId}")
    public Result<Brand> findBrandById(@PathVariable("brandId") String brandId) {
        Brand brand = brandService.findBrandById(brandId);
        return new Result<Brand>(true, StatusCode.OK, "查询成功", brand);
    }

    @PostMapping
    public Result<Brand> addBrand(@RequestBody Brand brand) {
        brandService.addBrand(brand);
        return new Result<Brand>(true, StatusCode.OK, "新增成功");
    }

    @PutMapping("{brandId}")
    public Result<Brand> modifyBrand(@PathVariable("brandId") String brandId, @RequestBody Brand brand) {
        brandService.modifyBrand(brandId, brand);
        return new Result<Brand>(true, StatusCode.OK, "修改成功");
    }

    @DeleteMapping("{brandId}")
    public Result<Brand> delBrandById(@PathVariable("brandId") Integer brandId) {
        brandService.delBrandById(brandId);
        return new Result<Brand>(true, StatusCode.OK, "shanchu成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param brand
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Brand>> findList(@RequestBody(required = false) Brand brand) {
        List<Brand> list = brandService.findList(brand);
        return new Result<List<Brand>>(true, StatusCode.OK, "tiaojian查询成功", list);
    }

    /***
     * 分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //分页查询
        PageInfo<Brand> pageInfo = brandService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * 分页搜索实现
     * @param brand
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) Brand brand, @PathVariable int page, @PathVariable int size) {
        //执行搜索
        PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }
}
